#!/usr/bin/env python
import os
import daemon
import tornado.web
import tornado.ioloop
import tornado.httpserver
import chart
import csvr

DIR = "/home/rain/nginx/nginx-rtmp/logs/"


class IndexHandler(tornado.web.RequestHandler):
    def get(self, liveid): 
        if os.path.exists(liveid) == False:
           self.write("Channel not exists")
           self.finish()
           return

        start = self.get_argument("start", default="00:00:00", strip=True)
        csvr.Parse_CSV(liveid, start)
        with open("/tmp/tmp.htm") as fs:
            data = fs.read()
            self.write(data)
        self.finish()

def APP():
    app = tornado.web.Application([
            (r"/qos/(\w+)" ,IndexHandler),
        ],
        static_path=DIR
       )
    httpServer = tornado.httpserver.HTTPServer(app)

    #httpServer.listen(8000)  #多进程改动原来的这一句

    httpServer.bind(9090)      #绑定在指定端口
    httpServer.start(1)
    tornado.ioloop.IOLoop.current().start()

if __name__=="__main__":
    with daemon.DaemonContext():
        os.chdir(DIR)
        APP()

